#include "FormJSONView.h"

FormJSONView::FormJSONView(QWidget * parent)
            : BaseFormWidget(parent)
            , m_eCurType(SELECT_TEXT_TYPE_SRC)
            , m_pFuncShow(nullptr)
            , m_pTextSrc(nullptr)
            , m_pTextCode(nullptr)
            , m_pJSONTreeView(nullptr)
            , m_pBtnTextSrc(nullptr)
            , m_pBtnTextCode(nullptr)
            , m_pBtnTextTree(nullptr)
{
    m_pFuncShow = new QVBoxLayout;
    addLayout2BaseLayout(m_pFuncShow);

    m_pTextSrc = new CJSONTextEdit(this);
    m_pTextCode = new CJSONTextEdit(this);
    m_pJSONTreeView = new JSONTreeView(this);

    QFontMetrics metrics(m_pTextSrc->font());
    m_pTextSrc->setTabStopWidth(4 * metrics.width(' '));

    m_pTextSrc->setPlaceholderText(QStringLiteral("输入区域可粘贴待解析JSON文本，或将JSON文件拖拽到输入区域中自动识别，或通过左上角打开按钮打开JSON文件获取内容"));
    m_pTextSrc->setFont(gstFont);
    m_pTextCode->setPlaceholderText(QStringLiteral("输入区域可粘贴待解析JSON文本，或将JSON文件拖拽到输入区域中自动识别，或通过左上角打开按钮打开JSON文件获取内容"));
    m_pTextCode->setFont(gstFont);
    m_pTextCode->setParseStatus(true);

    m_pFuncShow->addWidget(m_pTextSrc);
    m_pFuncShow->addWidget(m_pTextCode);
    m_pFuncShow->addWidget(m_pJSONTreeView);

    m_pJSONTreeView->hide();
    m_pTextCode->hide();

    // 创建工具栏
    newToolFunc();

    // 后续按钮靠右排列
    addBaseFuncStretch();

    // 添加顶部工具栏
    newBaseClose();

    // 所有按钮统一设置风格
    setBaseFont(gstFont);
}

// 文本输入框数据给入
void FormJSONView::slt_InsertText(QString &strContext)
{
    if(!strContext.isEmpty())
    {
        std::string strJSON = strContext.toStdString();
        if(false == isValidJSON(strJSON))
        {
            myMessageBoxShowOne(m_eProcType, MSG_SHOW_ERROR, QString("输入文本非有效JSON文本，请重新输入"));
            return;
        }

        m_pJSONTreeView->updateText(strJSON);
    }
}

// 拷贝当前数据到剪贴板
void FormJSONView::slt_Transfer()
{
    std::string strCurText;
    getgetCurTexChanged(strCurText);
    if(!strCurText.empty())
    {
        if(false == getUnformattedcJSON(strCurText))
        {
            QString strShow = QString("[%1] 报文去格式化失败，获取原始报文数据长度 %2 byte").arg(GetCurrentTime(SHOWTIME_TYPE_hhmmss), QString::number(strCurText.length()));
            myMessageBoxShowOne(m_eProcType, MSG_SHOW_ERROR, strShow);
            return;
        }

        std::string strResult;
        if(true == getJSON2Translation(strCurText, strResult))
        {
            QApplication::clipboard()->setText(QString::fromStdString(strResult));
            QString strShow = QString("[%1] 成功将结果复制到粘贴板，数据长度 %2 byte").arg(GetCurrentTime(SHOWTIME_TYPE_hhmmss), QString::number(strResult.length()));
            myMessageBoxShowOne(m_eProcType, MSG_SHOW_INFO, strShow);
        }
        else
        {
            QString strShow = QString("[%1] 报文转义失败，获取原始报文数据长度 %2 byte").arg(GetCurrentTime(SHOWTIME_TYPE_hhmmss), QString::number(strCurText.length()));
            myMessageBoxShowOne(m_eProcType, MSG_SHOW_ERROR, strShow);
        }
    }
}

// 拷贝当前数据到剪贴板
void FormJSONView::slt_CopySrc2Clipboard()
{
    std::string strCurText;
    getgetCurTexChanged(strCurText);

    if(!strCurText.empty())
    {
        QApplication::clipboard()->setText(QString::fromStdString(strCurText));
        QString strShow = QString("[%1] 成功将结果复制到粘贴板，数据长度 %2 byte").arg(GetCurrentTime(SHOWTIME_TYPE_hhmmss), QString::number(strCurText.length()));
        myMessageBoxShowOne(m_eProcType, MSG_SHOW_INFO, strShow);
    }
}

// 拷贝当前数据无格式化版本到剪贴板
void FormJSONView::slt_CopyUnformat2Clipboard()
{
    std::string strCurText;
    getgetCurTexChanged(strCurText);
    if(!strCurText.empty())
    {
        if(false == getUnformattedcJSON(strCurText))
        {
            QString strShow = QString("[%1] 报文去格式化失败，获取原始报文数据长度 %2 byte").arg(GetCurrentTime(SHOWTIME_TYPE_hhmmss), QString::number(strCurText.length()));
            myMessageBoxShowOne(m_eProcType, MSG_SHOW_ERROR, strShow);
        }
        else
        {
            QApplication::clipboard()->setText(QString::fromStdString(strCurText));
            QString strShow = QString("[%1] 成功将结果复制到粘贴板，数据长度 %2 byte").arg(GetCurrentTime(SHOWTIME_TYPE_hhmmss), QString::number(strCurText.length()));
            myMessageBoxShowOne(m_eProcType, MSG_SHOW_INFO, strShow);
        }
    }
}

// 清空当前数据
void FormJSONView::slt_Clear()
{
    if(nullptr != m_pTextSrc)
    {
        m_pTextSrc->clearAllData();
    }

    if(nullptr != m_pTextCode)
    {
        m_pTextCode->clearAllData();
    }

    if(nullptr != m_pJSONTreeView)
    {
        m_pJSONTreeView->clearAllData();
    }
}

// 展开所有节点
void FormJSONView::slt_ExpandAll()
{
    m_pJSONTreeView->expandAll();
}

// 收起所有节点
void FormJSONView::slt_CollapseAll()
{
    m_pJSONTreeView->collapseAll();
}

// 显示控件切换显示
void FormJSONView::slt_selectData(int idx)
{
    if(idx == m_eCurType)
    {
        return;
    }

    m_pBtnTextSrc->setStyleSheet(gStyle_PushButton_Yellow);
    m_pBtnTextCode->setStyleSheet(gStyle_PushButton_Yellow);
    m_pBtnTextTree->setStyleSheet(gStyle_PushButton_Yellow);

    m_pTextSrc->hide();
    m_pTextCode->hide();
    m_pJSONTreeView->hide();

    std::string strText;
    getgetCurTexChanged(strText);
    m_eCurType = (SELECT_TEXT_TYPE)idx;
    //qDebug() << "slt_selectData " << idx << "  strTextSize:" << strText.size();

    switch (idx)
    {
    case SELECT_TEXT_TYPE_SRC:
        {
            m_pBtnTextSrc->setStyleSheet(gStyle_PushButton_Blue);
            m_pTextSrc->show();
            m_pTextSrc->updateText(strText);
            break;
        }
    case SELECT_TEXT_TYPE_CODE:
        {
            m_pBtnTextCode->setStyleSheet(gStyle_PushButton_Blue);
            m_pTextCode->show();
            m_pTextCode->updateText(strText);
            break;
        }
    case SELECT_TEXT_TYPE_TREE:
        {
            m_pBtnTextTree->setStyleSheet(gStyle_PushButton_Blue);
            m_pJSONTreeView->show();
            m_pJSONTreeView->updateText(strText);
            break;
        }
    default:
        break;
    }
}

// 自动调整列宽比例
void FormJSONView::resizeEvent(QResizeEvent *event)
{
    if(nullptr == event)
    {
        return;
    }

    m_pJSONTreeView->setColumnWidth(0, frameGeometry().width() / 2);
}

// 创建工具栏
void FormJSONView::newToolFunc()
{
    QMenu *pActMenu = new QMenu(this);
    QAction *pActTransfer = new QAction("转义当前报文");
    QAction *pActCopySrc = new QAction("拷贝报文");
    QAction *pActCopyUnformat = new QAction("拷贝无格式报文");
    QAction *pActCleaer = new QAction("清空");

    pActTransfer->setIcon(QIcon(":/operationIcons/resource/operationIcons/purple_transfer.ico"));
    pActCopySrc->setIcon(QIcon(":/operationIcons/resource/operationIcons/file_copy.ico"));
    pActCopyUnformat->setIcon(QIcon(":/operationIcons/resource/operationIcons/file_copy.ico"));
    pActCleaer->setIcon(QIcon(":/operationIcons/resource/operationIcons/delete.ico"));

    // 菜单指向
    connect(pActTransfer, SIGNAL(triggered()), this, SLOT(slt_Transfer()));
    connect(pActCopySrc, SIGNAL(triggered()), this, SLOT(slt_CopySrc2Clipboard()));
    connect(pActCopyUnformat, SIGNAL(triggered()), this, SLOT(slt_CopyUnformat2Clipboard()));
    connect(pActCleaer, SIGNAL(triggered()), this, SLOT(slt_Clear()));

    pActMenu->addAction(pActTransfer);
    pActMenu->addSeparator();
    pActMenu->addAction(pActCopySrc);
    pActMenu->addAction(pActCopyUnformat);
    pActMenu->addSeparator();
    pActMenu->addAction(pActCleaer);
    pActMenu->setFont(gstFont);

    // 设置菜单按钮
    QPushButton *pMenu = new QPushButton(this);
    pMenu->setMenu(pActMenu);
    pMenu->setFlat(true);
    pMenu->setIcon(QIcon(QString(":/operationIcons/resource/operationIcons/footprints.ico")));
    pMenu->setIconSize(QSize(22, 22));
    pMenu->setStatusTip(QString("打开设置菜单"));
    addBaseToolFunc(pMenu);
    addBaseFuncStretch();

    m_pBtnTextSrc = newFuncWithIndex(QString("源文本"), SELECT_TEXT_TYPE_SRC, QString("输入报文源文本"), QIcon(QString("")));
    m_pBtnTextCode = newFuncWithIndex(QString("格式化"), SELECT_TEXT_TYPE_CODE, QString("格式化话文本展示"), QIcon(QString("")));
    m_pBtnTextTree = newFuncWithIndex(QString("树状图"), SELECT_TEXT_TYPE_TREE, QString("报文树形图展示"), QIcon(QString("")));

    connect(m_pBtnTextSrc, SIGNAL(SendClicked(int)), this, SLOT(slt_selectData(int)));
    connect(m_pBtnTextCode, SIGNAL(SendClicked(int)), this, SLOT(slt_selectData(int)));
    connect(m_pBtnTextTree, SIGNAL(SendClicked(int)), this, SLOT(slt_selectData(int)));

    m_pBtnTextSrc->setStyleSheet(gStyle_PushButton_Blue);
    m_pBtnTextCode->setStyleSheet(gStyle_PushButton_Yellow);
    m_pBtnTextTree->setStyleSheet(gStyle_PushButton_Yellow);

    addBaseToolFunc(m_pBtnTextSrc, true);
    addBaseToolFunc(m_pBtnTextCode, true);
    addBaseToolFunc(m_pBtnTextTree, true);
    m_pBtnTextSrc->setMinimumWidth(80);
    m_pBtnTextCode->setMinimumWidth(80);
    m_pBtnTextTree->setMinimumWidth(80);
}

// 根据当前选项获取数据
bool FormJSONView::getgetCurTexChanged(std::string &strCurText)
{
    bool bStatus = false;
    switch (m_eCurType)
    {
    case SELECT_TEXT_TYPE_SRC:
        {
            bStatus = m_pTextSrc->getSourceData(strCurText);
            break;
        }
    case SELECT_TEXT_TYPE_CODE:
        {
            bStatus = m_pTextCode->getSourceData(strCurText);
            break;
        }
    case SELECT_TEXT_TYPE_TREE:
        {
            bStatus = m_pJSONTreeView->getSourceData(strCurText);
            break;
        }
    default:
        break;
    }

    return bStatus;
}

// 按比例重置列宽
void FormJSONView::resizeWindows()
{
    return;
}
